<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>函数返回值详解 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter08/section03.html" />
    
    
    <link rel="prev" href="../chapter08/section01.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="8.2"
        data-chapter-title="函数返回值详解"
        data-filepath="chapter08/section02.md"
        data-basepath=".."
        data-revision="Sun Feb 09 2020 13:02:11 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="../chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        什么是函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="../chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        函数的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="../chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        函数的参数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="../chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        函数的返回值
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="../chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        文档注释
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="../chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        函数练习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="../chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        函数调用函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08/index.html">
            
                
                    <a href="../chapter08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        函数高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="chapter08/section01.html">
            
                
                    <a href="../chapter08/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        局部变量和全局变量
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="8.2" data-path="chapter08/section02.html">
            
                
                    <a href="../chapter08/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        函数返回值详解
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="chapter08/section03.html">
            
                
                    <a href="../chapter08/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        函数参数详解
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="chapter08/section04.html">
            
                
                    <a href="../chapter08/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        可变数据类型和不可变数据类型
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;">&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;</h2>
<p>&#x5728;python&#x4E2D;&#x6211;&#x4EEC;&#x600E;&#x6837;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;&#xFF1F;</p>
<h3 id="&#x4E00;&#x3001;&#x591A;&#x4E2A;return">&#x4E00;&#x3001;&#x591A;&#x4E2A;return?</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_nums</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&quot;---1---&quot;</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>  <span class="hljs-comment"># &#x51FD;&#x6570;&#x4E2D;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E0D;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x56E0;&#x4E3A;return&#x9664;&#x4E86;&#x80FD;&#x591F;&#x5C06;&#x6570;&#x636E;&#x8FD4;&#x56DE;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x9690;&#x85CF;&#x7684;&#x529F;&#x80FD;&#xFF1A;&#x7ED3;&#x675F;&#x51FD;&#x6570;</span>
    print(<span class="hljs-string">&quot;---2---&quot;</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>
    print(<span class="hljs-string">&quot;---3---&quot;</span>)
</code></pre>
<h3 id="&#x603B;&#x7ED3;1&#xFF1A;">&#x603B;&#x7ED3;1&#xFF1A;</h3>
<ul>
<li><p>&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;return&#x8BED;&#x53E5;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;return&#x8BED;&#x53E5;&#x88AB;&#x6267;&#x884C;&#x5230;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5C31;&#x4F1A;&#x7ED3;&#x675F;&#x4E86;&#xFF0C;&#x56E0;&#x6B64;&#x540E;&#x9762;&#x7684;return&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x7528;&#x5904;</p>
</li>
<li><p>&#x5982;&#x679C;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x4E3A;&#x5982;&#x4E0B;&#xFF0C;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#x56E0;&#x4E3A;&#x4E0D;&#x540C;&#x7684;&#x573A;&#x666F;&#x4E0B;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x7684;return</p>
<pre><code class="lang-python">  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_nums</span><span class="hljs-params">(num)</span>:</span>
      print(<span class="hljs-string">&quot;---1---&quot;</span>)
      <span class="hljs-keyword">if</span> num == <span class="hljs-number">100</span>:
          print(<span class="hljs-string">&quot;---2---&quot;</span>)
          <span class="hljs-keyword">return</span> num+<span class="hljs-number">1</span>  <span class="hljs-comment"># &#x51FD;&#x6570;&#x4E2D;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E0D;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x56E0;&#x4E3A;return&#x9664;&#x4E86;&#x80FD;&#x591F;&#x5C06;&#x6570;&#x636E;&#x8FD4;&#x56DE;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x9690;&#x85CF;&#x7684;&#x529F;&#x80FD;&#xFF1A;&#x7ED3;&#x675F;&#x51FD;&#x6570;</span>
      <span class="hljs-keyword">else</span>:
          print(<span class="hljs-string">&quot;---3---&quot;</span>)
          <span class="hljs-keyword">return</span> num+<span class="hljs-number">2</span>
      print(<span class="hljs-string">&quot;---4---&quot;</span>)

  result1 = create_nums(<span class="hljs-number">100</span>)
  print(result1)  <span class="hljs-comment"># &#x6253;&#x5370;101</span>
  result2 = create_nums(<span class="hljs-number">200</span>)
  print(result2)  <span class="hljs-comment"># &#x6253;&#x5370;202</span>
</code></pre>
</li>
</ul>
<h3 id="&#x4E8C;&#x3001;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x7684;&#x65B9;&#x5F0F;">&#x4E8C;&#x3001;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x7684;&#x65B9;&#x5F0F;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">divid</span><span class="hljs-params">(a, b)</span>:</span>
    shang = a//b
    yushu = a%b 
    <span class="hljs-keyword">return</span> shang, yushu  <span class="hljs-comment">#&#x9ED8;&#x8BA4;&#x662F;&#x5143;&#x7EC4;</span>

result = divid(<span class="hljs-number">5</span>, <span class="hljs-number">2</span>)
print(result)  <span class="hljs-comment"># &#x8F93;&#x51FA;(2, 1)</span>
</code></pre>
<h3 id="&#x603B;&#x7ED3;2&#xFF1A;">&#x603B;&#x7ED3;2&#xFF1A;</h3>
<ul>
<li><p>return&#x540E;&#x9762;&#x53EF;&#x4EE5;&#x662F;&#x5143;&#x7EC4;&#xFF0C;&#x5217;&#x8868;&#x3001;&#x5B57;&#x5178;&#x7B49;&#xFF0C;&#x53EA;&#x8981;&#x662F;&#x80FD;&#x591F;&#x5B58;&#x50A8;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4E00;&#x6B21;&#x6027;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-python">      <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">function</span><span class="hljs-params">()</span>:</span>
          <span class="hljs-comment"># return [1, 2, 3]</span>
          <span class="hljs-comment"># return (1, 2, 3)</span>
          <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;num1&quot;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&quot;num2&quot;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&quot;num3&quot;</span>: <span class="hljs-number">3</span>}
</code></pre>
</li>
<li><p>&#x5982;&#x679C;return&#x540E;&#x9762;&#x6709;&#x591A;&#x4E2A;&#x6570;&#x636E;&#xFF0C;&#x90A3;&#x4E48;&#x9ED8;&#x8BA4;&#x662F;&#x5143;&#x7EC4;&#x3002;</p>
</li>
</ul>
<h2 id="&#x5BF9;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x76F4;&#x63A5;&#x62C6;&#x5305;">&#x5BF9;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x76F4;&#x63A5;&#x62C6;&#x5305;</h2>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_my_info</span><span class="hljs-params">()</span>:</span>
    high = <span class="hljs-number">178</span>
    weight = <span class="hljs-number">100</span>
    age = <span class="hljs-number">18</span>
    <span class="hljs-keyword">return</span> high, weight, age  <span class="hljs-comment"># &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E09;&#x4E2A;&#x6570;&#x636E;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x6253;&#x5305;&#x4E3A;&#x5143;&#x7EC4;</span>

<span class="hljs-comment"># result = get_my_info()  # result &#x63A5;&#x6536;&#x5230;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;</span>
<span class="hljs-comment"># print(result)</span>

my_high, my_weight, my_age = get_my_info()  <span class="hljs-comment"># &#x76F4;&#x63A5;&#x628A;&#x5143;&#x7EC4;&#x62C6;&#x5206;&#x4E3A;&#x4E09;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x4F7F;&#x7528;&#xFF0C;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;</span>
print(my_high)
print(my_weight)
print(my_age)
</code></pre>
<h3 id="&#x603B;&#x7ED3;&#xFF1A;">&#x603B;&#x7ED3;&#xFF1A;</h3>
<ul>
<li>&#x62C6;&#x5305;&#x65F6;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x9700;&#x8981;&#x62C6;&#x7684;&#x6570;&#x636E;&#x7684;&#x4E2A;&#x6570;&#x8981;&#x4E0E;&#x53D8;&#x91CF;&#x7684;&#x4E2A;&#x6570;&#x76F8;&#x540C;&#xFF0C;&#x5426;&#x5219;&#x7A0B;&#x5E8F;&#x4F1A;&#x5F02;&#x5E38;</li>
<li>&#x9664;&#x4E86;&#x5BF9;&#x5143;&#x7EC4;&#x62C6;&#x5305;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x5BF9;&#x5217;&#x8868;&#x3001;&#x5B57;&#x5178;&#x7B49;&#x62C6;&#x5305;</li>
</ul>
<pre><code class="lang-python">In [<span class="hljs-number">17</span>]: a, b = (<span class="hljs-number">11</span>, <span class="hljs-number">22</span>)
In [<span class="hljs-number">18</span>]: a
Out[<span class="hljs-number">18</span>]: <span class="hljs-number">11</span>
In [<span class="hljs-number">19</span>]: b
Out[<span class="hljs-number">19</span>]: <span class="hljs-number">22</span>

In [<span class="hljs-number">20</span>]: a, b = [<span class="hljs-number">11</span>, <span class="hljs-number">22</span>]
In [<span class="hljs-number">21</span>]: a
Out[<span class="hljs-number">21</span>]: <span class="hljs-number">11</span>
In [<span class="hljs-number">22</span>]: b
Out[<span class="hljs-number">22</span>]: <span class="hljs-number">22</span>

In [<span class="hljs-number">23</span>]: a, b = {<span class="hljs-string">&quot;m&quot;</span>:<span class="hljs-number">11</span>, <span class="hljs-string">&quot;n&quot;</span>:<span class="hljs-number">22</span>}  <span class="hljs-comment"># &#x53D6;&#x51FA;&#x6765;&#x7684;&#x662F;key&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x952E;&#x503C;&#x5BF9;</span>
In [<span class="hljs-number">24</span>]: a
Out[<span class="hljs-number">24</span>]: <span class="hljs-string">&apos;m&apos;</span>
In [<span class="hljs-number">25</span>]: b
Out[<span class="hljs-number">25</span>]: <span class="hljs-string">&apos;n&apos;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter08/section01.html" class="navigation navigation-prev " aria-label="Previous page: 局部变量和全局变量"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter08/section03.html" class="navigation navigation-next " aria-label="Next page: 函数参数详解"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
